//
//  SettlementCountView.m
//  HondaPro_2
//
//  Created by Eric on 13-6-3.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "SettlementCountView.h"
#import "DiscountItem.h"

@interface SettlementCountView ()

/** 结算日 */
@property (weak, nonatomic) IBOutlet UILabel *settlementDateLbl;
/** 交车日 */
@property (weak, nonatomic) IBOutlet UILabel *handCarDateLbl;
/** 交车人 */
@property (weak, nonatomic) IBOutlet UILabel *handCarPerson;

/** 工费应收 */
@property (weak, nonatomic) IBOutlet UILabel *workChargeLbl;
/** 工费折扣 */
@property (weak, nonatomic) IBOutlet UILabel *workDiscountLbl;
/** 工费优惠 */
@property (weak, nonatomic) IBOutlet UILabel *workDiscountChargeLbl;

/** 零件应收 */
@property (weak, nonatomic) IBOutlet UILabel *partChargeLbl;
/** 零件折扣 */
@property (weak, nonatomic) IBOutlet UILabel *partDiscountLbl;
/** 零件优惠 */
@property (weak, nonatomic) IBOutlet UILabel *partDiscountChargeLbl;

/** 附加项目应收 */
@property (weak, nonatomic) IBOutlet UILabel *attachChargeLbl;
/** 附加项目折扣 */
@property (weak, nonatomic) IBOutlet UILabel *attachDiscountLbl;
/** 附加项目优惠 */
@property (weak, nonatomic) IBOutlet UILabel *attachDiscountChargeLbl;

/** 合计金额 */
@property (weak, nonatomic) IBOutlet UILabel *totalAmountLbl;
/** 折扣合计 */
@property (weak, nonatomic) IBOutlet UILabel *discountAmountLbl;
/** 实际结算金额 */
@property (weak, nonatomic) IBOutlet UILabel *actualAmountLbl;

@property(nonatomic,strong)NSMutableDictionary *discountModeDic;

@property(nonatomic,strong)DiscountItem *discountMode;

@end

@implementation SettlementCountView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        NSArray  *viewArray= [[NSBundle mainBundle]loadNibNamed:@"SettlementCountView" owner:nil options:nil];
        self = [viewArray firstObject];
        self.frame = frame;
    }
    return self;
}

-(void)setJobSheetData:(NSDictionary *)jobSheetData{
    _jobSheetData = jobSheetData;
    NSMutableDictionary *baseInfo = jobSheetData[@"TT_REPAIR_ORDER"][0];//基本信息
    
    self.settlementDateLbl.text = baseInfo[@"BA_TIME"];//结算日期
    self.handCarDateLbl.text = baseInfo[@"DEL_DATE"];//交车日期
    self.handCarPerson.text = baseInfo[@"USER_NAME"];//交车人
    
    self.workChargeLbl.text = [baseInfo[@"LABOUR_AMOUNT"]stringValue];//工时费
    self.partChargeLbl.text = [baseInfo[@"REPAIR_PART_AMOUNT"]stringValue];//零件费
    self.attachChargeLbl.text = [baseInfo[@"ADD_ITEM_AMOUNT"]stringValue];//附加项目费
    
    //折扣
    NSString *discountCode = baseInfo[@"DISCOUNT_MODE_CODE"];
    self.discountMode = self.discountModeDic[discountCode];

    //计算维修项目部分
    double repairAmount = [self computRepairProjectList];
    
    //计算零件部分
    double partAmount = [self computPartList];
    
    //计算附加项目件部分
    double attachAmount = [self computAttachProjectList];

    double totalAmount = repairAmount+partAmount+attachAmount;
    //总金额
    self.totalAmountLbl.text = [NSString stringWithFormat:@"%.2f",totalAmount];

    double repairDiscount = [self.workDiscountChargeLbl.text doubleValue];
    double partDiscount = [self.partDiscountChargeLbl.text doubleValue];
    double attachDiscount = [self.attachDiscountChargeLbl.text doubleValue];
    double totalDiscount = repairDiscount+partDiscount+attachDiscount;
    //折扣合计
    self.discountAmountLbl.text = [NSString stringWithFormat:@"%.2f",totalDiscount];
    
    //实际结算金额
    self.actualAmountLbl.text = [NSString stringWithFormat:@"%.2f",totalAmount - totalDiscount];
}

/** 计算维修项目部分 */
-(double)computRepairProjectList{
    NSMutableArray *projectList = self.jobSheetData[@"TT_RO_LABOUR"];
    double totalAmout = 0;//总费用
    //计算折扣
    double discount = 0;
    if (projectList&&projectList.count>0) {
        double workHourCharge;
        for (NSDictionary *data in projectList) {
            workHourCharge = [data[@"LABOUR_AMOUNT"]doubleValue];
            totalAmout += workHourCharge;
        }
        
        if (self.discountMode) {
            discount = self.discountMode.repairDiscount;
            if (discount == 1) {
                discount = 0;
            }
        }
    }

    self.workChargeLbl.text = [NSString stringWithFormat:@"%.2f",totalAmout];
    self.workDiscountChargeLbl.text = [NSString stringWithFormat:@"%.2f",totalAmout *discount];
    if (discount == 0) {
        discount = 1;
    }
    self.workDiscountLbl.text = [NSString stringWithFormat:@"%.1f%%",discount*100];
    return totalAmout;
}

/** 计算零件部分 */
-(double)computPartList{
    NSMutableArray *partList = self.jobSheetData[@"TT_RO_REPAIR_PART"];
    //计算折扣
    double discount = 0;
    double totalAmout = 0;//总费用
    if (partList&&partList.count>0) {
        double partCharge = 0;//零件费
        for (NSDictionary *data in partList) {
            partCharge = [data[@"PART_SALES_AMOUNT"]doubleValue];
            totalAmout += partCharge;
        }
        if (self.discountMode) {
            discount = self.discountMode.partDiscount;
            if (discount == 1) {
                discount = 0;
            }
        }
    }
    self.partChargeLbl.text = [NSString stringWithFormat:@"%.2f",totalAmout];
    self.partDiscountChargeLbl.text = [NSString stringWithFormat:@"%.2f",totalAmout * discount];
    if (discount == 0) {
        discount = 1;
    }
    self.partDiscountLbl.text = [NSString stringWithFormat:@"%.1f%%",discount*100];
    return totalAmout;
}

/** 计算附加项目件部分 */
-(double)computAttachProjectList{
    NSMutableArray *attachList = self.jobSheetData[@"TT_RO_ADD_ITEM"];
    double discount = 0;
    double totalAmout = 0;//总费用
    if (attachList&&attachList.count>0) {
        double attachCharge = 0;//零件费
        for (NSDictionary *data in attachList) {
            attachCharge = [data[@"ADD_ITEM_AMOUNT"]doubleValue];
            totalAmout += attachCharge;
        }
        if (self.discountMode) {
            discount = self.discountMode.attachDiscount;
            if (discount == 1) {
                discount = 0;
            }
        }
    }
    self.attachChargeLbl.text = [NSString stringWithFormat:@"%.2f",totalAmout];
    self.attachDiscountChargeLbl.text = [NSString stringWithFormat:@"%.2f",totalAmout * discount];
    if (discount == 0) {
        discount = 1;
    }
    self.attachDiscountLbl.text = [NSString stringWithFormat:@"%.1f%%",discount*100];
    return totalAmout;
}

-(NSMutableDictionary *)discountModeDic{
    if (_discountModeDic == nil) {
        _discountModeDic = [NSMutableDictionary dictionary];
        NSMutableArray *arr = [self readershuDICimform][@"TM_DISCOUNT_MODEL"];
        for (NSDictionary *dic in arr) {
            DiscountItem *discount = [[DiscountItem alloc]init];
            discount.name = dic[@"DISCOUNT_MODE_NAME"];
            discount.code = dic[@"DISCOUNT_MODE_CODE"];
            discount.repairDiscount = [dic[@"LABOUR_AMOUNT_DISCOUNT"]doubleValue];
            discount.partDiscount = [dic[@"REPAIR_PART_DISCOUNT"]doubleValue];
            discount.attachDiscount = [dic[@"ADD_ITEM_DISCOUNT"]doubleValue];
            [_discountModeDic setObject:discount forKey:discount.code];
        }
    }
    return _discountModeDic;
}

-(NSMutableDictionary *)readershuDICimform
{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path=[paths objectAtIndex:0];
    NSString *str=[path stringByAppendingPathComponent:@"Dic.text"];
    dic=[NSMutableDictionary dictionaryWithContentsOfFile:str];
    //nslog(@"%@",dic);
    return dic;
}
@end
